<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="pageParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="changeValue"
        />
        <!-- 树形组件 -->
        <el-tree
          :data="depts"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          @node-click="nodeClick"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button v-permission="addpermission" size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button v-permission="importemployee" size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <el-button v-permission="导航员工" size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{ row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column prop="" label="操作" width="280px">
            <template v-slot="{ row }">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="btnRole(row.id)">角色</el-button>
              <el-dialog :visible.sync="showRoleDialog" title="分配角色">
                <!-- 弹层内容 -->
                <el-checkbox-group v-model="roleIds">
                  <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
                </el-checkbox-group>
                <el-row slot="footer" type="flex" justify="center">
                  <el-col :span="6">
                    <el-button type="primary" size="mini" @click="btnRoleOK">确定</el-button>
                    <el-button size="mini" @click="showRoleDialog = false">取消</el-button>
                  </el-col>
                </el-row>
                <!-- checkbox -->

              </el-dialog>
              <el-popconfirm title="确认删除该行数据吗？" @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="center">
          <el-pagination

            layout="total,prev, pager, next"
            :total="total"
            :page-size="pageParams.pagesize"
            :current-page="pageParams.page"
            @current-change="currentChange"
          />
        </el-row>
        <!-- 分页 -->
      </div>
      <import-excel :show-excel-dialog.sync="showExcelDialog" />
    </div>
  </div>
</template>

<script>
import { getDepartment } from '@/api/department'//
import { getEmployeeList, exportEmployee, delEmployee, getEnableRoleList, getEmployeeDetail, assignRole } from '@/api/employee'// 员工列表
import { transListToTreeData } from '@/utils'
import FileSaver from 'file-saver'
import ImportExcel from './components/import-excel.vue'
export default {
  name: 'Employee',
  components: {
    // eslint-disable-next-line vue/no-unused-components
    ImportExcel
  },
  data() {
    return {
      showPermissionDialog: false,
      permissionData: [],
      currentUserId: null,
      showRoleDialog: false, // 用来控制角色弹层的显示
      roleList: [], // 接收角色列表
      roleIds: [], // 用来双向绑定数据的
      depts: [], // 组织数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      pageParams: {
        page: 1, // 第几页
        pagesize: 10, // 每条多少页
        departmentId: 1, // 部门id 传智教育部门的id 1 , 最顶层部门,所有员工肯定属于它内部,  ==  不筛选 全部查询
        keyword: '' // 模糊查询
      },
      list: [],
      total: 0,
      showExcelDialog: false // 控制excel的弹层显示和隐藏 // 记录员工的总数
    }
  },
  created() {
    this.getDepartment()
    this.getEmployeeList()
  },
  methods: {
    // 左树
    async getDepartment() {
      const res = await getDepartment()
      this.depts = transListToTreeData(res.data, 0)
    },
    // 右表
    async getEmployeeList() {
      const res = await getEmployeeList(this.pageParams)
      this.list = res.data.rows
      console.log(res.data)
      this.total = res.data.total
    },
    // 分页
    currentChange(page) {
      this.pageParams.page = page
      this.getEmployeeList()
    },
    // 模糊搜索
    changeValue() {
      clearTimeout(this.timeId)
      this.timeId = setTimeout(() => {
        this.getEmployeeList()
      }, 1000)
    },
    nodeClick(row) {
      // 页码重置
      this.pageParams.page = 1
      // 点击节点部门 id
      this.pageParams.departmentId = row.id
      // 发送请求
      this.getEmployeeList()
    },
    // 导出
    async exportEmployee() {
      const res = await exportEmployee()
      FileSaver.saveAs(res, '员工信息表.xlsx')
    },
    async confirmDel(id) {
      const res = await delEmployee(id)
      console.log(res)
      if (this.list.length === 1 && this.pageParams.page > 1) {
        this.pageParams.page--
      }
      this.getEmployeeList()
      this.$message.success('删除成功')
    },
    async btnRole(id) {
      const res = await getEnableRoleList()
      this.roleList = res.data
      console.log(this.roleList)
      this.currentUserId = id
      console.log(this.currentUserId)
      // 获取角色分配权限点
      const res1 = await getEmployeeDetail(id)
      console.log(res1)
      // 赋值 - 数据回填
      this.roleIds = res1.data.roleIds
      console.log(this.roleIds)
      this.showRoleDialog = true
    },
    async btnRoleOK() {
      const res = await assignRole({ id: this.currentUserId, roleIds: this.roleIds })
      console.log(res)
      console.log(this.roleList)
      this.$message.success('分配成功')
      this.showRoleDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;

  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }

  .right {
    flex: 1;
    padding: 20px;

    .opeate-tools {
      margin: 10px;
    }

    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display: inline-block;
    }
  }
}</style>
